﻿using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Pay.Token.Controllers.Api
{
    /// <summary>
    /// 商品
    /// </summary>
    [RoutePrefix("product")]
    public class ProductController :   BaseController {
        private ICategoryService _iCategoryService;
        private IProductService _iProductService;
        private IMerchantService _iMerchantService;
        public ProductController(ICategoryService iCategoryService, IProductService iProductService, IMerchantService iMerchantService) {
            _iCategoryService = iCategoryService;
            _iProductService = iProductService;
            _iMerchantService = iMerchantService;
        }
        /// <summary>
        /// 商品分类
        /// </summary>
        /// <returns></returns>
        [Route("get_category"), HttpGet]
        public ResultMessage<List<token_category_dto>> GetCategory() {
           var result  =_iCategoryService.GetTokenFirstDepthCatagory();
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 商品分类(一级分类 2级分类)
        /// </summary>
        /// <returns></returns>
        [Route("get_categorys"), HttpGet]
        public ResultMessage<List<category_ex>> GetCategorys() {
            var merchant = _iMerchantService.Get(t_RequestMethod.merchant_no);
            var list = _iCategoryService.GetAll(merchant.id, 1);
            return ResultToJson.Json(list);
        }
        /// <summary>
        /// 新增修改商品
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("add_or_update_product"), HttpPost]
        public ResultMessage<bool> AddOrUpdateProduct(token_product_dto model) {
            return ResultToJson.Json<bool>("此接口已停用，请升级新版本");
        }
        /// <summary>
        /// 新增修改商品
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("save_product"), HttpPost]
        public ResultMessage<bool> SaveProduct(token_product_dto model) {
            if (!ModelState.IsValid)
                return ResultToJson.Json<bool>(ModelError);
            model.merchant_id = UserId;
            bool result = _iProductService.SaveTokenProduct(model);
            return ResultToJson.Json(result ? "保存成功" : "保存失败", result);
        }

        /// <summary>
        /// 获取商品信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("get_product"), HttpGet]
        public ResultMessage<token_product_dto> GetProduct(long id) {
           var result= _iProductService.GetTokenProduct(id);
            var category = _iCategoryService.Get(result.category_id);
            if (category != null) {
                if (category.parent_id > 0) {
                    //如果分类有上级时，就是二级的分类
                    var pcategory = _iCategoryService.Get(category.parent_id);
                    result.parent_category_id = pcategory.id;
                    result.parent_category_name = pcategory.name;
                } else {
                    result.parent_category_id = category.id;
                    result.parent_category_name = category.name;
                    result.category_id = 0;
                    result.category_name = string.Empty;
                }
            }
            return ResultToJson.Json(result);
        }
    }
}
